package com.didispace;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Hello world!
 *
 */
public class App {
    static HashMap<String, String> maps = new HashMap<>();

    static{
        maps.put("pvt-uat-","pvt-test-");
        maps.put("https://pvt-uat.qiweioa.com.cn:30323","https://pvt-test.qiweioa.com.cn:31709");
        maps.put("https://pvt-uat.qiweioa.com.cn:31704","https://pvt-test.qiweioa.com.cn:31709");
        maps.put("spring.datasource.url=jdbc:mysql://192.168.83.21:3306/pvt-uat-conductor","spring.datasource.url=jdbc:mysql://192.168.83.21:3306/pvt-test-conductor");
        maps.put("spring.datasource.username=pvt-uat","spring.datasource.username=pvt-test");
        maps.put("spring.datasource.password=Do1admin_uat","spring.datasource.password=Do1admin_test");
    }

    public static void main(String[] args) {
        //加载nacos配置，根据 com.didispace.App#maps 替换对应的内容
        String zipFilePath = "/Users/seven/Downloads/pvt-uat.zip"; // 指定ZIP文件的路径
        String outputFolder = "/Users/seven/Downloads/pvt-test"; // 指定解压后文件存放的文件夹

        try {
            unzip(zipFilePath, outputFolder);
            replaceInFiles(outputFolder);
            System.out.println("完成解压和替换操作。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void unzip(String zipFilePath, String outputFolder) throws IOException {
        Path destDir = Paths.get(outputFolder);
        Files.createDirectories(destDir);

        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath))) {
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                Path newFile = destDir.resolve(entry.getName());
                if (entry.isDirectory()) {
                    Files.createDirectories(newFile);
                } else {
                    Files.createDirectories(newFile.getParent());
                    Files.copy(zis, newFile, StandardCopyOption.REPLACE_EXISTING);
                }
                zis.closeEntry();
            }
        }
    }

    private static void replaceInFiles(String folderPath) throws IOException {
        Path start = Paths.get(folderPath);
        Files.walk(start)
                .filter(Files::isRegularFile)
                .forEach(filePath -> {
                    try {
                        String content = new String(Files.readAllBytes(filePath));
                        for(String fromStr:maps.keySet()){
                            content = content.replace(fromStr, maps.get(fromStr));
                        }
                        Files.write(filePath, content.getBytes());
                    } catch (IOException e) {
                        System.err.println("无法处理文件: " + filePath);
                        e.printStackTrace();
                    }
                });
    }
}
